﻿using System;
using System.Collections;

namespace Eneta.Portal.Forum.Core.Domain.Collections
{
    [Serializable]
    public class ObjectCollection : CollectionBase
    {
        // Methods
        public ObjectCollection()
        {
        }

        public ObjectCollection(ObjectCollection coll)
        {
            InnerList.AddRange(coll);
        }

        public virtual void Add(object person)
        {
            List.Add(person);
        }

        public bool Contains(object person)
        {
            return base.List.Contains(person);
        }

        public int IndexOf(object person)
        {
            return List.IndexOf(person);
        }

        public static ObjectCollection ReadOnly(ObjectCollection coll)
        {
            return new ReadOnlyPersonCollection(coll);
        }

        public virtual void Remove(object person)
        {
            List.Remove(person);
        }

        // Properties
        public object this[int index]
        {
            get
            {
                return List[index];
            }
            set
            {
                List[index] = value;
            }
        }

        // Nested Types
        private sealed class ReadOnlyPersonCollection : ObjectCollection
        {
            // Fields
            private const string ERROR_STRING = "Collection is read-only.";

            // Methods
            internal ReadOnlyPersonCollection(ObjectCollection coll)
                : base(coll)
            {
            }

            public override void Add(object person)
            {
                throw new NotSupportedException("Collection is read-only.");
            }

            protected override void OnClear()
            {
                throw new NotSupportedException("Collection is read-only.");
            }

            protected override void OnInsert(int index, object value)
            {
                throw new NotSupportedException("Collection is read-only.");
            }

            protected override void OnRemove(int index, object value)
            {
                throw new NotSupportedException("Collection is read-only.");
            }

            protected override void OnSet(int index, object oldValue, object newValue)
            {
                throw new NotSupportedException("Collection is read-only.");
            }

            public override void Remove(object person)
            {
                throw new NotSupportedException("Collection is read-only.");
            }
        }
    }
}
